package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.dao.MovieMapper;
import com.zretc.entity.Movie;
import com.zretc.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MovieServiceImpl implements MovieService {

    @Autowired
    private MovieMapper movieMapper;

    @Override
    public PageInfo<Movie> getMovieByStatusByPage(Map<String, String> params) {
        int pageNum = Integer.parseInt(params.get("pageNum"));
        int pageSize = Integer.parseInt(params.get("pageSize"));
        int showStatus = Integer.parseInt(params.get("showStatus"));
        // 开始分页
        PageHelper.startPage(pageNum,pageSize);// 对传递给dao方法中参数进行处理

        List<Movie> movieByStatus = movieMapper.findMovieByStatus(showStatus);

        // 3.包装到PageInfo对象
        PageInfo<Movie> pageInfo = new PageInfo<>(movieByStatus);


        return pageInfo;
    }

    /**
     * 戴世星  2020/7/22
     * 不同组合条件查询显示电影
     * 根据状态 showStatus 分类显示
     * 1.无条件查询
     * 2.电影类型精确查询  movieType
     * 3.电影地区精确查询  areaName
     * 4.地区+类型查询
     * 5.电影名模糊查询    movieName
     *
     * @param params
     * @return
     */
    @Override
    public PageInfo<Movie> getMovieCondition(Map<String, String> params) {

        int pageNum = Integer.parseInt(params.get("pageNum"));
        int pageSize = Integer.parseInt(params.get("pageSize"));
        int showStatus = Integer.parseInt(params.get("showStatus"));
        String movieType = params.get("movieType");
        String areaName = params.get("areaName");
        String movieName = params.get("movieName");

        // 开始分页
        PageHelper.startPage(pageNum,pageSize);

        // 对传递给dao方法中参数进行处理
        Map<String, String> map = new HashMap<>();
        map.put("showStatus",Integer.toString(showStatus));
        if ("全部".equals(movieType) || "".equals(movieType) || movieType == null){
            map.put("movieType",null);
        } else {
            map.put("movieType",movieType);
        }

        if ("全部".equals(areaName) || "".equals(areaName) || areaName == null){
            map.put("areaName",null);
        } else {
            map.put("areaName",areaName);
        }

        if ("".equals(movieName) || movieName == null){
            map.put("movieName",null);
        } else {
            map.put("movieName","%"+movieName+"%");
        }

        List<Movie> movieList = movieMapper.findMovieCondition(map);
        // 包装到PageInfo对象
        PageInfo<Movie> pageInfo = new PageInfo<>(movieList);

        return pageInfo;
    }
}
